package za.co.johanmynhardt.web;

import java.io.IOException;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import za.co.johanmynhardt.web.feed.FeedService;

/**
 * @author johan.mynhardt@thumbtribe.mobi
 * @version 2011.1220
 */
@WebServlet(urlPatterns = "/feed/atom")
public class AtomServlet extends HttpServlet {
	private Logger logger = Logger.getLogger(AtomServlet.class.getName());
	
	@EJB
	private FeedService feedService;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		SyndFeed feed = feedService.getAtomFeed();

		SyndFeedOutput syndFeedOutput = new SyndFeedOutput();

		try {
			syndFeedOutput.output(feed, resp.getWriter());
		} catch (FeedException e) {
			logger.severe("Could not write feed to stream: " + e.getMessage());
		}
	}
}
